import 'dart:async';

import 'package:flutter/material.dart';
import 'package:my_flutter_github/common/Config.dart';
import 'package:my_flutter_github/common/DataResult.dart';
import 'package:my_flutter_github/widgets/PullLoadWidget.dart';

///上下拉刷新列表的通用State
abstract class BaseListState<T extends StatefulWidget> extends State<T>  with AutomaticKeepAliveClientMixin {

  bool isShow = false;

  bool isLoading = false;

  int pageIndex = 1;

  final List dataList = List();

  final PullLoadWidgetControl pullLoadWidgetControl = PullLoadWidgetControl();

  final GlobalKey<RefreshIndicatorState> refreshIndicatorKey = GlobalKey<RefreshIndicatorState>();

  showRefreshLoading(){
    Future.delayed(Duration(seconds: 0),(){
        refreshIndicatorKey.currentState.show().then((e) {
            //do nothing
        });
    });
  }

  ///虽然没有protected修饰，但是有protected注解
  @protected
  handleRefreshResult(DataResult result) {
    if(result != null && result.result) {
      pullLoadWidgetControl.dataList.clear();
      if(isShow) {
        setState(() {
          pullLoadWidgetControl.dataList.addAll(result.data);
        });
      }
    }
  }

  @protected
  handleLoadMoreResult(DataResult result) {
    if(isShow) {
      setState(() {
        pullLoadWidgetControl.needLoadMore = (result != null && result.data != null && result.data.length == Config.PAGE_SIZE);
      });
    }
  }


  @protected
  Future<Null> onRefresh() async {
    if(isLoading) {
      return null;
    }

    print("onRefresh");

    isLoading = true;
    pageIndex = 1;

    var result = await requestRefresh();
    handleRefreshResult(result);
    handleLoadMoreResult(result);

    if(result.next != null) {
      var next = await result.next;
      handleRefreshResult(next);
      handleLoadMoreResult(next);
    }
    isLoading = false;
    return null;
  }

  @protected
  Future<Null> onLoadMore() async {
    if(isLoading) {
      return null;
    }

    print("onLoadMore");

    isLoading = true;
    pageIndex++;
    var result = await requestLoadMore();
    if(result != null && result.result) {
      if(isShow) {
        setState(() {
          pullLoadWidgetControl.dataList.addAll(result.data);
        });
      }
    }

    handleLoadMoreResult(result);
    isLoading = false;
    return null;
  }

  @protected
  clearData() {
    if(isShow) {
      setState(() {
        pullLoadWidgetControl.dataList.clear();
      });
    }
  }

  ///下拉刷新
  @protected
  requestRefresh() async {}

  ///上拉加载更多
  @protected
  requestLoadMore() async {}

  @override
  bool get wantKeepAlive {
    return true;
  }
  //简写
//  @override
//  bool get wantKeepAlive => true;

  ///是否需要进入自动刷新
  @protected
  bool get autoRefresh => false;
  //等价于
//   bool autoRefresh() {
//     return false;
//   }

  ///是否需要头部
  @protected
  bool get needHeader => true;

  List get getDataList => dataList;

  @override
  void initState() {
    isShow = true;
    super.initState();
    pullLoadWidgetControl.needHeader = needHeader;
    if(pullLoadWidgetControl.dataList.length == 0 && autoRefresh) {
      showRefreshLoading();
    }
  }

  @override
  void dispose() {
    isShow = false;
    isLoading = false;
    super.dispose();
  }
}

